<template>
  <el-dialog :title="showTitle" :visible="showDialog" :close-on-click-modal="false" @close="closeDialog">
    <el-form ref="roleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="form.description" placeholder="请输入角色描述" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="CreatingRoles">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { CreatingARoleAPI, RoleDetailsAPI, EditorRoleAPI } from '@/api/setting'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不可为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '新建角色'
    }
  },
  methods: {
    // 退出
    closeDialog() {
    // 修改父组件的值
      this.$emit('update:showDialog', false)
      // 清空表单正则
      this.$refs.roleForm.resetFields()
      // 清空表单
      this.form = {
        name: '',
        description: ''
      }
    },
    // 新建角色
    CreatingRoles() {
      // 兜底校验
      this.$refs.roleForm.validate(async valid => {
        // 有不对的立马退出
        if (!valid) return
        if (this.form.id) {
          const res = await EditorRoleAPI(this.form)
          console.log(res)
        } else {
          // 拿到接口的值
          const res = await CreatingARoleAPI(this.form)
          // 提示
          this.$message.success(res.message)
        }
        // 退出
        this.closeDialog()
        this.$parent.getRoleList()
      })
    },
    // 角色详情
    async RoleDetail() {
      const res = await RoleDetailsAPI(this.id)
      this.form = res.data
      console.log(res)
    }
  }

}
</script>

<style>

</style>
